package thread;

import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yinlei
 * @date 2018/7/12
 * @since 1.0.0
 */
public class FileLockDemo {

    public static void main(String[] args) {
        String path = "/Users/yinlei/temp/java/status.txt";

        new Thread(() -> {
            while (true) {
                String data = "初始数据";
                File f = new File(path);
                InputStream in = null;
                try {
                    in = new FileInputStream(f);
                    data = IOUtils.toString(in);
                } catch (IOException e) {
                    e.printStackTrace();
                    data += "读取失败";
                } finally {
                    IOUtils.closeQuietly(in);
                }
                System.out.println(data);

                try {
                    Thread.sleep((long) (5000 * Math.random()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {

                FileLock lock = null;
                RandomAccessFile file = null;
                FileChannel channel = null;
                try {
                    file = new RandomAccessFile(path, "rw");
                    channel = file.getChannel();
                    while (true) {
                        try {
                            lock = channel.tryLock(0, 1000, true);
                            break;
                        } catch (Exception e1) {
                            System.out.println("获取锁失败" + e1.getMessage());
                        }
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

//                    file.seek(0);
                    file.setLength(0);
                    file.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).getBytes());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (lock != null) {
                        try {
                            lock.release();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    IOUtils.closeQuietly(channel);
                    IOUtils.closeQuietly(file);
                }

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        Thread.currentThread().interrupt();
        System.exit(0);
        System.out.println("ok");
    }
}
